
/*
 * @Descripttion: 栈封装
 * @Author: liuWei37853264@qq.com
 * @Date: 2021-07-16 15:10:07
 * @LastEditors: liuWei
 * @LastEditTime: 2021-07-22 10:02:35
 */

interface stackType {
    push: Function,
    pop: () => any,
    peek: Function,
    isEmpty: () => boolean,
    size: () => number,
    toString: () => number
}


class Stack implements stackType {
    public stack: any[]
    constructor() {
        this.stack = []


    }

    /**
     * 栈顶推入元素
     * @param ele 
     */
    push(ele: any) {
        this.stack.push(ele)
    }

    /**
    * 栈顶移除元素 
    */
    pop(): any {
        return this.stack.pop()
    }

    /**
     * 返回栈顶元素
     * @returns  {any}  
     */
    peek(): any {
        const index = this.stack.length - 1
        return this.stack[index]
    }

    /**
     * 栈是否为空
     * @returns {boolean}  
     */
    isEmpty(): boolean {
        return this.stack.length === 0
    }

    /**
    * 返回栈元素的个数
    * @returns {number}  
    */
    size(): number {
        return this.stack.length
    }

    /**
     * 返回栈元素的个数
     * @returns {string}  
    */
    toString(): string {
        return this.stack.join(",")
    }

}

export { Stack }